package tw.com.msig.b2c.car.setting.web;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tw.com.msig.b2c.car.common.web.APILibDirector;
import tw.com.msig.b2c.car.common.web.BaseAction;
import tw.com.msig.b2c.car.setting.service.AnnounceService;
import tw.com.msig.b2c.car.setting.service.CityService;
import tw.com.msig.b2c.car.setting.vo.AnnounceVo;

public class AnnounceAction extends BaseAction {
	private Logger log = LoggerFactory.getLogger(this.getClass());

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String forwardPage = "list";

		String sActionType = request.getParameter("sActionType");
		log.debug("{}", sActionType.toString());
		if ("list".equals(sActionType)) {
			forwardPage = list(request, response);
		} else if ("add".equals(sActionType)) {
			forwardPage = add(request, response);
		} else if ("edit".equals(sActionType)) {
			forwardPage = edit(request, response);
		} else if ("delete".equals(sActionType)) {
			forwardPage = delete(request, response);
		} else if ("insert".equals(sActionType)) {
			forwardPage = insert(request, response);
		} else if ("update".equals(sActionType)) {
			forwardPage = update(request, response);
		}
		return mapping.findForward(forwardPage);
	}

	private String update(HttpServletRequest request,
			HttpServletResponse response) {
		AnnounceService annService = (AnnounceService) APILibDirector
				.getSpringBean("announceService");

		AnnounceVo anoVo = new AnnounceVo();
		anoVo.setId(NumberUtils.toLong(request.getParameter("id")));
		anoVo.setTitle(request.getParameter("title"));
		anoVo.setContent(request.getParameter("content"));
		anoVo.setModifier(((String) request.getSession()
				.getAttribute("b2cUser")));
		anoVo.setModifyTime((new Timestamp(new Date().getTime())));
		log.debug("{---------------------------------}", anoVo.toString());
		annService.update(anoVo);

		return list(request, response);
	}

	private String insert(HttpServletRequest request,
			HttpServletResponse response) {
		AnnounceService annService = (AnnounceService) APILibDirector
				.getSpringBean("announceService");

		AnnounceVo anoVo = new AnnounceVo();
		
		anoVo.setTitle(request.getParameter("title"));
		anoVo.setContent(request.getParameter("content"));
		anoVo.setCreator((String) request.getSession().getAttribute("b2cUser"));
		anoVo.setCreateTime(new Timestamp(new Date().getDate()));

		annService.insert(anoVo);

		return list(request, response);
	}

	private String delete(HttpServletRequest request,
			HttpServletResponse response) {
		AnnounceService annService = (AnnounceService) APILibDirector
				.getSpringBean("announceService");
		AnnounceVo anoVo = new AnnounceVo();
		log.debug("{}", request.getParameter("id"));
		anoVo.setId(NumberUtils.toLong(request.getParameter("id")));

		annService.delete(anoVo);
		return list(request, response);
	}

	private String edit(HttpServletRequest request, HttpServletResponse response) {
		AnnounceService annService = (AnnounceService) APILibDirector
				.getSpringBean("announceService");

		Long id = NumberUtils.toLong(request.getParameter("id"));
		AnnounceVo annVo = annService.getById(id);

		System.out.println("------------Announce Edit------------------------");

		log.debug("{}", annVo.toString());

		request.setAttribute("model", annVo);

		log.debug("{}", annVo.toString());

		return "edit";
	}

	private String add(HttpServletRequest request, HttpServletResponse response) {
		return "add";
	}

	public String list(HttpServletRequest request, HttpServletResponse response) {

		AnnounceService annService = (AnnounceService) APILibDirector
				.getSpringBean("announceService");
		List<AnnounceVo> announceVos = annService.getAll();
		request.setAttribute("models", announceVos);

		return "list";
	}
}
